{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fe2cec19",
   "metadata": {},
   "source": [
    "## Homework1_python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67d259b4",
   "metadata": {},
   "source": [
    "### 6. 算法 1\n",
    "\n",
    "给一个数字列表，将其按照由大到小的顺序排列\n",
    "\n",
    "例如： 1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ec4b93bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[34, 10, 9, 9, 8, 5, 5, 4, 2, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "num=[1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0]\n",
    "num.sort(reverse=True)\n",
    "print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb428b5",
   "metadata": {},
   "source": [
    "### 7. 算法 2\n",
    "\n",
    "编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性：\n",
    "\n",
    "* 每行的元素从左到右升序排列。\n",
    "* 每列的元素从上到下升序排列。\n",
    "\n",
    "示例: 现有矩阵 matrix 如下：\n",
    "* 给定 target = 5，返回 true。\n",
    "* 给定 target = 20，返回 false。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106b708d",
   "metadata": {},
   "source": [
    "[\n",
    "[1, 4, 7, 11, 15],\n",
    "[2, 5, 8, 12, 19],\n",
    "[3, 6, 9, 16, 22],\n",
    "[10, 13, 14, 17, 24],\n",
    "[18, 21, 23, 26, 30]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "78c9262a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the matrix：[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ]\n",
      "Enter the target number：21\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "in1=input(\"Enter the matrix：\")\n",
    "in1=eval(in1)\n",
    "x=int(input(\"Enter the target number：\"))\n",
    "m=len(in1)#获取矩阵行数\n",
    "n=len(in1[0])#获取矩阵列数\n",
    "c=max(m,n)\n",
    "max0=max(max(in1))\n",
    "min0=min(min(in1))\n",
    "\n",
    "def check(i):#缩小区域之后遍历这个区域内查找target number\n",
    "    for j in range(0, i + 1):\n",
    "        for k in range(0, i + 1):\n",
    "            if x == in1[k][j]:\n",
    "                return 1\n",
    "                break\n",
    "            else:\n",
    "                k = k + 1\n",
    "        j = j + 1\n",
    "        if j == i and k==i:\n",
    "            return 0\n",
    "        \n",
    "if x>max0 or x<min0:\n",
    "    print(\"False\")\n",
    "else:\n",
    "    for i in range(0,c):\n",
    "        if x > in1[i][i]:#对所有对角线上的元素进行判断\n",
    "            i = i + 1\n",
    "            continue\n",
    "        elif x == in1[i][i]:\n",
    "            print(\"True\")\n",
    "            break\n",
    "        else:#当x小于对角线上的某个元素\n",
    "            if check(i)==1:\n",
    "                print(\"True\")\n",
    "                break\n",
    "            if check(i)==0:\n",
    "                print(\"False\")\n",
    "                break\n",
    "        if i == m-1:\n",
    "            print(\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a5d8e9e",
   "metadata": {},
   "source": [
    "### 8. 应用 1\n",
    "\n",
    "做为 Apple Store App 独立开发者，你要搞限时促销，为你的应用生成激活码（或者优惠券），使用 Python 如何生成 200 个激活码（或者优惠券）？\n",
    "\n",
    "需要考虑什么是激活码？有什么特性？例如KR603guyVvR是一个激活码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1326121c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Code1: O2Uk7y8lPIo\n",
      "Code2: i7pGkwEb0F3\n",
      "Code3: 65KDUfyemp7\n",
      "Code4: 7XB9gmI6tTA\n",
      "Code5: AMDaf1Xh3q8\n",
      "Code6: 2GTwhtA76DW\n",
      "Code7: 0wJ7g9WyYAK\n",
      "Code8: Xoa40pyldG3\n",
      "Code9: T3Eux5S1fbH\n",
      "Code10: 9XTyRh7Vo4b\n",
      "Code11: ISiHz5O0F1s\n",
      "Code12: 14tfJXKCZo8\n",
      "Code13: xPHYA70NhZ9\n",
      "Code14: aE1T4bj3oDJ\n",
      "Code15: Y6rtaU74iMb\n",
      "Code16: 4Pd5blqRK8M\n",
      "Code17: W41hDmHyl3v\n",
      "Code18: QA6iNwYX2l1\n",
      "Code19: Pp97Eg0XZvL\n",
      "Code20: tCHaK2zN01u\n",
      "Code21: yefHTQd194h\n",
      "Code22: uWLg73h5meR\n",
      "Code23: kV3R7BCF9XI\n",
      "Code24: 7CXar9fTA1J\n",
      "Code25: J47vQEN8oIF\n",
      "Code26: TlC9HKz1ph5\n",
      "Code27: I0Ckb9a6YnB\n",
      "Code28: iL07SkU5cln\n",
      "Code29: 8R0hoEWwz4C\n",
      "Code30: 782uqyzBWcl\n",
      "Code31: 3k1tMfVPC0S\n",
      "Code32: LXqZJ8Y1Qi2\n",
      "Code33: 5vuJ1Ij3Ycq\n",
      "Code34: Iov6H7C8AmV\n",
      "Code35: 3D7AMNEaP8O\n",
      "Code36: KNhG8w0J4Ev\n",
      "Code37: 9sm6xt7qLBE\n",
      "Code38: A4OX2T5lnKi\n",
      "Code39: f1HLdNGz52C\n",
      "Code40: JlLj3wi7Nx2\n",
      "Code41: Teobv0BE5F1\n",
      "Code42: HK2GIiQUw31\n",
      "Code43: i3XgKE1y2Ds\n",
      "Code44: fRH4Mas71Bk\n",
      "Code45: 1pRCg5Wc3as\n",
      "Code46: pw20gLItOv3\n",
      "Code47: esE9Ikoc1X8\n",
      "Code48: 0jFJ8VZgo1r\n",
      "Code49: WBISrRz360q\n",
      "Code50: C6bmu59PQET\n",
      "Code51: scIB609VTHm\n",
      "Code52: dkAsl2P8b6n\n",
      "Code53: PkUR7JWy34M\n",
      "Code54: 62FiOKDNj8L\n",
      "Code55: roB1Lw6WIc5\n",
      "Code56: 9tpoqxI1hQ6\n",
      "Code57: f0PYO3H9kJj\n",
      "Code58: t21xFwHriy4\n",
      "Code59: t5MeKFkIs67\n",
      "Code60: 6jzeC8wUHF1\n",
      "Code61: M8NIGSFl2i6\n",
      "Code62: Y9qehN6of8Z\n",
      "Code63: WmYtVb17GR2\n",
      "Code64: HfWvz8V7pU5\n",
      "Code65: x86JUwP5Dkp\n",
      "Code66: Ih9jJ8dLv1f\n",
      "Code67: AOZ1TQCG0J7\n",
      "Code68: ej6Y1BxV5KG\n",
      "Code69: WwoQ17yBkT8\n",
      "Code70: xo8CZX53WJI\n",
      "Code71: 5J28XAiZgtp\n",
      "Code72: szWA2Ei1ml9\n",
      "Code73: OT1PA42YEKi\n",
      "Code74: 8vawmZ3PoF6\n",
      "Code75: 72w5ijPmZAV\n",
      "Code76: uCze4R8q6Nh\n",
      "Code77: HOK30Xx6oMv\n",
      "Code78: S1lNjAP9bz0\n",
      "Code79: 8bgU9k7yWQZ\n",
      "Code80: 0aFmsVA37ql\n",
      "Code81: AZQ3K2E7SHd\n",
      "Code82: hmIYlE9Ak63\n",
      "Code83: fTPuDIp405V\n",
      "Code84: X5OBS8I7gGu\n",
      "Code85: 9gzElVs38DL\n",
      "Code86: OHY0W9MCJ3T\n",
      "Code87: 9Yg5TMPD2iQ\n",
      "Code88: y9Bpvif8m0X\n",
      "Code89: PN6OF3Yt5Kn\n",
      "Code90: 5r4dOb6CJlN\n",
      "Code91: R0zTtW12iJf\n",
      "Code92: A8lBb4Vg3kW\n",
      "Code93: v5z19CBjOiZ\n",
      "Code94: uhi1WO6fdw8\n",
      "Code95: mZn7U4Fbw8X\n",
      "Code96: e4Sja7k9KBU\n",
      "Code97: MdzO7PJGp51\n",
      "Code98: 1w7IF4XdkjY\n",
      "Code99: Itl125YXEuW\n",
      "Code100: rJax35Z4gTG\n",
      "Code101: J3DYfnVR6g5\n",
      "Code102: nLmYb0DfN61\n",
      "Code103: nlSCys1I5h6\n",
      "Code104: ZjeV3Qm64Df\n",
      "Code105: a0p28jlWbkD\n",
      "Code106: 5WPk2bx9nSC\n",
      "Code107: u3vcd4fn5NG\n",
      "Code108: XY6IRZkb53S\n",
      "Code109: I1X2NnR8EwC\n",
      "Code110: j1OEnaU76ld\n",
      "Code111: F17RyZskjM2\n",
      "Code112: 2OHqI8W9Fgk\n",
      "Code113: nB61zlfN9eR\n",
      "Code114: cV4G7S6jRNU\n",
      "Code115: KHdvZmlF354\n",
      "Code116: XpHV632IjGl\n",
      "Code117: SpLrs3M94nd\n",
      "Code118: K8cGoke09YE\n",
      "Code119: cPryj92KO8s\n",
      "Code120: M06cKH5hpal\n",
      "Code121: d7r2qinp5Ik\n",
      "Code122: 6PxB4s8pzHi\n",
      "Code123: M16DLKawl9W\n",
      "Code124: nsVi84gFZz3\n",
      "Code125: fb2q3MBcU5h\n",
      "Code126: R3ZfCF41NaU\n",
      "Code127: QCuR8pT91fc\n",
      "Code128: hn7V8FGoY3Q\n",
      "Code129: 0nr53BLJSdl\n",
      "Code130: Cq4Rp9d3Moe\n",
      "Code131: SGJK6BW57lP\n",
      "Code132: J2cQUFaw16t\n",
      "Code133: ksCz45yxt6o\n",
      "Code134: BIYjyiP78J3\n",
      "Code135: foTUZ52v1IR\n",
      "Code136: mD2XWof67ip\n",
      "Code137: j08hcin9HOv\n",
      "Code138: Q6JK4U3jhrF\n",
      "Code139: WbuxOqQ2f90\n",
      "Code140: rtU4sJD61ko\n",
      "Code141: fAXn6a01YbB\n",
      "Code142: ozW5v10hREs\n",
      "Code143: hekv3fD7Z4R\n",
      "Code144: LogY6r39lcR\n",
      "Code145: MTnClho1p62\n",
      "Code146: lU5uJcIw1n4\n",
      "Code147: 4P7BTUMej5w\n",
      "Code148: spEoTG38P0M\n",
      "Code149: 76o3PHLxetu\n",
      "Code150: AvsgPISH105\n",
      "Code151: p27bAEXLZt0\n",
      "Code152: 13efcX4jVkI\n",
      "Code153: 3qGo19QxTkc\n",
      "Code154: 8ntgf7pCQK6\n",
      "Code155: WOvUelm718s\n",
      "Code156: 7F1iwfcM3yh\n",
      "Code157: D97AUOkGl4r\n",
      "Code158: 4EV7bOlkd3T\n",
      "Code159: 3vMXiht8f2e\n",
      "Code160: 50e6fMhugxG\n",
      "Code161: Dn6BAc78QMK\n",
      "Code162: LDZ7kYxi93u\n",
      "Code163: WqXm135SrMn\n",
      "Code164: Y5eP7yMUvO2\n",
      "Code165: My1VvTeG7p2\n",
      "Code166: N1dyDYsH95I\n",
      "Code167: 84q3bndpYPk\n",
      "Code168: GJAZs9eE52U\n",
      "Code169: q4R1FyxvVs3\n",
      "Code170: eaCYS7z2fI5\n",
      "Code171: qvVX73Mujz0\n",
      "Code172: 5ISuQ7hZU9F\n",
      "Code173: 2YHS4Fs8fEg\n",
      "Code174: H8yB4Jl6CtI\n",
      "Code175: 0d6bfDqUzG2\n",
      "Code176: W1w8dcfJg7X\n",
      "Code177: MgclIS9Qz70\n",
      "Code178: q6lDhR43KsX\n",
      "Code179: PRt3o4KMi0U\n",
      "Code180: zhS4c7awsA5\n",
      "Code181: Eh2Uk0q3Djw\n",
      "Code182: r8V9ct0uGRi\n",
      "Code183: O1WdUj6YL2D\n",
      "Code184: GQ76vJ5NPaE\n",
      "Code185: 7jAB9x0lrMG\n",
      "Code186: fSKjV1hs95U\n",
      "Code187: Xbv2BYPR36c\n",
      "Code188: 6TAV1p4rHnh\n",
      "Code189: Gm9pxMSJ7e1\n",
      "Code190: 4h2zjQGg7bN\n",
      "Code191: Joljed5GH63\n",
      "Code192: px2z0htkgl8\n",
      "Code193: rf4T7W0LVCO\n",
      "Code194: HaFn2Rcq79b\n",
      "Code195: n8fJy7VeG2A\n",
      "Code196: 8faFyGN9n4x\n",
      "Code197: BPZ71yNog6d\n",
      "Code198: y31QIoeLk5s\n",
      "Code199: FO53Nitr2Kv\n",
      "Code200: 5NmEL34tuZd\n"
     ]
    }
   ],
   "source": [
    "#首先将0-9和大写A-Z定义在一个列表里面\n",
    "warehouse_num=[]\n",
    "warehouse_letter=[]\n",
    "for i in range(48,58):#输入数字0-9\n",
    "    warehouse_num.append(chr(i))\n",
    "for i in range(65,91):#输入大写字母A-Z\n",
    "    warehouse_letter.append(chr(i))\n",
    "for i in range(97,123):#输入小写写字母a-z\n",
    "    warehouse_letter.append(chr(i))\n",
    "\n",
    "#从warehouse里随机抽取10个元素组成乱码\n",
    "import random\n",
    "\n",
    "for i in range(1,201):\n",
    "    code=[]\n",
    "    while len(code)<3:#在数字中只取三个\n",
    "        choose=random.choice(warehouse_num)\n",
    "        if choose in code:\n",
    "            pass\n",
    "        else:\n",
    "            code.append(choose)\n",
    "    while len(code)<11:#在字母中取7个\n",
    "        choose=random.choice(warehouse_letter)\n",
    "        if choose in code:\n",
    "            pass\n",
    "        else:\n",
    "            code.append(choose)\n",
    "    random.shuffle(code)#将得到的数字和字母打乱\n",
    "    code_join=\"\".join(code)#合并列表\n",
    "    print(\"Code\"+str(i)+\": \"+code_join)  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1379c466",
   "metadata": {},
   "source": [
    "### 9. 应用 2\n",
    "\n",
    "需要把某个目录下面所有的某种类型的文件找到。 例如把c:下面所有的.dll文件找到。需要注意的是，需要递归到每一个目录去查找。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1036b9d1",
   "metadata": {},
   "source": [
    "程序中所定义的变量意义：\n",
    "* directory-主路径\n",
    "* filetype-指定文件类型\n",
    "* filelist-目标类型文件列表（路径+文件名）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02b4a33d",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "#程序代码\n",
    "import os\n",
    "def SearchFiles(directory, filetype):\n",
    "    filelist=[]\n",
    "    for root, subDirs, files in os.walk(directory):\n",
    "        for filename in files:\n",
    "            if filename.endswith(filetype):\n",
    "                filelist.append(os.path.join(root,filename))\n",
    "    return filelist\n",
    "directory = 'C:/'\n",
    "filetype ='.dll'\n",
    "filelist = SearchFiles(directory,filetype)\n",
    "for i in range(len(filelist)):\n",
    "    print(filelist[i])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "64e05e0f",
   "metadata": {},
   "source": [
    "**pycharm运行结果**\n",
    "\n",
    "![%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202021-09-18%20094602.png](https://gitee.com/fyf393/homework_2019300696_-fu-yifang.git)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6b6efbef",
   "metadata": {},
   "source": [
    "### 10. 应用 3\n",
    "\n",
    "你有个目录，里面是程序（假如是C或者是Python），统计一下你写过多少行代码。包括空行和注释，但是要分别（例如C程序多少行，Python程序多少行，等等）列出来\n",
    "\n",
    "**文件目录**\n",
    "![%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202021-09-18%20093738.png](https://gitee.com/fyf393/homework_2019300696_-fu-yifang/blob/master/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202021-09-18%20093738.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a4152f7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Add file: 0_test.py\n",
      "Add file: 2 variable and data.py\n",
      "Add file: 3.1 3.2 list_basic.py\n",
      "Add file: 3.3 list_organizing.py\n",
      "Add file: 4.1_list_circulation.py\n",
      "Add file: 4.3-4.6_list_operational.py\n",
      "Add file: 5.2_if sentence_basic.py\n",
      "Calculating  C:\\sec/0_test.py\n",
      "Calculating  C:\\sec/2 variable and data.py\n",
      "Calculating  C:\\sec/3.1 3.2 list_basic.py\n",
      "Calculating  C:\\sec/3.3 list_organizing.py\n",
      "Calculating  C:\\sec/4.1_list_circulation.py\n",
      "Calculating  C:\\sec/4.3-4.6_list_operational.py\n",
      "Calculating  C:\\sec/5.2_if sentence_basic.py\n",
      "所有python代码的行数：160\n",
      "其中包括的注释行数：2\n",
      "其中空格行数：7\n",
      "\t\n",
      "Add file: 003.cbp\n",
      "Add file: 004.cbp\n",
      "Calculating  C:\\sec/003.cbp\n",
      "Calculating  C:\\sec/004.cbp\n",
      "所有c代码的行数：114\n",
      "其中包括的注释行数：0\n",
      "其中空格行数：2\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "def get_py_file(path):#获取py文件的路径\n",
    "    file_list_py=[]\n",
    "    for file in os.listdir(path):\n",
    "        if os.path.splitext(file)[1]==\".py\":#python文件\n",
    "          file_list_py.append(path+'/'+file)\n",
    "          print(\"Add file:\",file)\n",
    "    return file_list_py\n",
    "\n",
    "def get_c_file(path):#获取c文件的路径\n",
    "    file_list_c=[]\n",
    "    for file in os.listdir(path):\n",
    "        if os.path.splitext(file)[1]==\".cbp\":#codeblocks的c文件\n",
    "          file_list_c.append(path+'/'+file)\n",
    "          print(\"Add file:\",file)\n",
    "    return file_list_c\n",
    "\n",
    "def get_rows(file):#统计一个文件里面的代码行、空行、注释行\n",
    "    with open(file,encoding=\"utf8\") as f:\n",
    "        lines=f.readline()\n",
    "        rows=len(lines)\n",
    "        vv=0\n",
    "        space=0\n",
    "        for line in lines:\n",
    "            if line==\"\\n\":#空行\n",
    "                space+=1\n",
    "            if line[0]==\"#\":#注释行\n",
    "                vv+=1\n",
    "        data={\"rows\":rows,\"vv\":vv,\"space\":space}\n",
    "    return data\n",
    "\n",
    "def run(path):#获取总行数\n",
    "    file_list=get_py_file(path)\n",
    "    all_rows=0\n",
    "    all_vuui=0\n",
    "    all_space=0\n",
    "    for file in file_list:\n",
    "        data=get_rows(file)\n",
    "        print(\"Calculating \",file)\n",
    "        rows=data.get(\"rows\")\n",
    "        vv=data.get(\"vv\")\n",
    "        space=data.get(\"space\")\n",
    "        all_rows+=rows\n",
    "        all_space+=space\n",
    "        all_vuui+=vv\n",
    "    print('所有python代码的行数：{}\\n其中包括的注释行数：{}\\n其中空格行数：{}'.format(all_rows, all_vuui, all_space))\n",
    "    print(\"\\t\")\n",
    "\n",
    "    file_list = get_c_file(path)\n",
    "    all_rows = 0\n",
    "    all_vuui = 0\n",
    "    all_space = 0\n",
    "    for file in file_list:\n",
    "        data = get_rows(file)\n",
    "        print(\"Calculating \", file)\n",
    "        rows = data.get(\"rows\")\n",
    "        vv = data.get(\"vv\")\n",
    "        space = data.get(\"space\")\n",
    "        all_rows += rows\n",
    "        all_space += space\n",
    "        all_vuui += vv\n",
    "    print('所有c代码的行数：{}\\n其中包括的注释行数：{}\\n其中空格行数：{}'.format(all_rows, all_vuui, all_space))\n",
    "\n",
    "if __name__ =='__main__':\n",
    "    filepath = \"C:\\sec\"\n",
    "    run(path=filepath)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
